package com.smbms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.utils.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * smbms_user表的控制层
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    /**
     * 分页条件查询用户列表-使用mybatis分页插件
     */
    @ResponseBody
    @RequestMapping("/getUserList2/{pageNow}/{pageSize}")
    public Object getUserList2(@PathVariable("pageNow") Integer pageNow,@PathVariable Integer pageSize, String userName, Integer userRole){

        //条件查询用户列表
        PageHelper.startPage(1, 10);
        List<User> userList=userService.getUserList2(userName,userRole);
        PageInfo pageInfo = new PageInfo(userList);

        //封装数据
        Map<String,Object> map=new HashMap<>();
        map.put("flag",true);
        map.put("msg","用户列表分页条件查询成功");
        map.put("userList",userList);
        //返回结果数据
        Map<String,Object> result=new HashMap<>();
        result.put("flag",true);
        result.put("msg","用户列表查询success");
       // result.put("count",count);
        result.put("data",map);

        return pageInfo;
    }

    /**
     *退出
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Object logout(HttpSession session){
        session.invalidate();
        Map<String,Object> result=new HashMap<>();
        result.put("flag",true);
        result.put("msg","退出成功..");
        return result;
    }

    /**
     * 分页条件查询用户列表
     */
    @ResponseBody
    @RequestMapping("/getUserList/{pageNow}/{pageSize}")
    public Object getUserList(@PathVariable("pageNow") Integer pageNow,@PathVariable Integer pageSize, String userName, Integer userRole){
        //查询用户总数量
        int count=userService.getUserCount(userName,userRole);
        //条件查询用户列表
        int start=(pageNow-1)*pageSize;
        List<User> userList=userService.getUserList(userName,userRole,start,pageSize);

        //封装数据
        Map<String,Object> map=new HashMap<>();
        map.put("flag",true);
        map.put("msg","用户列表分页条件查询成功");
        map.put("userList",userList);
        //返回结果数据
        Map<String,Object> result=new HashMap<>();
        result.put("flag",true);
        result.put("msg","用户列表查询success");
        result.put("count",count);
        result.put("data",map);

        return result;
    }
    /**
     * 删除用户
     */
    @ResponseBody
    @RequestMapping("/delUser/{id}")
    public Object delUser(@PathVariable Integer id){
        int count=userService.delUser(id);

        Map<String,Object> result=new HashMap<>();
        if (count>0){
            result.put("flag",true);
            result.put("msg","用户删除成功");
        }else {
            result.put("flag",false);
            result.put("msg","用户删除失败");
        }

        return result;
    }
    /**
     * 用户编码校验
     */
    @ResponseBody
    @RequestMapping("/checkUserCode/{userCode}")
    public Object getCountByUserCode(@PathVariable String userCode){
        int count=userService.getCountByUserCode(userCode);

        Map<String,Object> result=new HashMap<>();
            result.put("flag",true);
            result.put("msg","根据userCode查询用户数量成功");
            result.put("data",count);

        return result;
    }
    /**
     * 上传用户头像
     */
    @ResponseBody
    @RequestMapping("/uploadHeadImg")
    public Object uploadHeadImg(MultipartFile headImg, HttpServletRequest request) throws IOException {
        String fileName = FileUtil.uploadFile(headImg, request);

        Map<String,Object> result=new HashMap<>();
        result.put("flag",true);
        result.put("msg","文件上传成功");
        result.put("data",fileName);

        return result;
    }
    /**
     * 添加用户
     */
    @ResponseBody
    @RequestMapping("/addUser")
    public Object addUser(User user,HttpSession session){
        User login_user = (User) session.getAttribute("LOGIN_USER");

        user.setCreatedBy(login_user.getId());
        //添加用户
        int count=userService.addUser(user);
        //封装数据
        Map<String,Object> result=new HashMap<>();
        if (count>0){
            result.put("flag",true);
            result.put("msg","用户添加成功");
            result.put("data",count);
        }else {
            result.put("flag",false);
            result.put("msg","用户添加失败");
        }
        return result;
    }

    /**
     *根据id查询用户-作数据回显
     */
    @ResponseBody
    @RequestMapping("/getUserById/{id}")
    public Object getUserById(@PathVariable Integer id){
        User user=userService.getUserById(id);
        Map<String,Object> result=new HashMap<>();
        if (user!=null){
            result.put("flag",true);
            result.put("msg","用户查询成功");
            result.put("data",user);
        }else {
            result.put("flag",false);
            result.put("msg","用户查询失败");
        }

        return result;
    }
    /**
     * 修改用户校验用户编码
     */
    @ResponseBody
    @RequestMapping("/getCountByUserCodeAndId/{userCode}/{id}")
    public Object getCountByUserCodeAndId(@PathVariable String userCode,@PathVariable Integer id){
        int count=userService.getCountByUserCodeAndId(userCode,id);

        Map<String,Object> result=new HashMap<>();
        result.put("flag",true);
        result.put("msg","根据userCode查询用户数量成功");
        result.put("data",count);

        return result;
    }
    /**
     * 修改用户
     */
    @ResponseBody
    @RequestMapping("/updateUser")
    public Object updateUser(User user,HttpSession session){
        Map<String,Object> result=new HashMap<>();

        User login_user = (User) session.getAttribute("LOGIN_USER");
        user.setModifyBy(login_user.getId());
        int count=userService.updateUser(user);
        if (count>0){
            result.put("flag",true);
            result.put("msg","修改用户success");
            result.put("data",count);
        }else {
            result.put("flag",false);
            result.put("msg","修改用户failed");
        }
        return result;
    }
}
